package com.qdlc.p2p.manage.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.qdlc.p2p.common.constant.BorrowRepaymentStatus;
import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.BorrowConfigService;
import com.qdlc.p2p.biz.service.BorrowRepaymentService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dto.BorrowConfig;
import com.qdlc.p2p.dal.dto.BorrowRepayment;
import com.qdlc.p2p.dal.model.BorrowRepaymentModel;

/**
 * 借贷管理-逾期信息管理
 * 
 * @author cx
 * @version 2.0 2014-11-17
 */
@SuppressWarnings("rawtypes")
public class ManageBorrowLateAction extends BaseAction implements
		ModelDriven<BorrowRepaymentModel> {

	BorrowRepaymentModel model = new BorrowRepaymentModel();
	@Resource
	private BorrowRepaymentService borrowRepaymentService;
	@Resource
	private BorrowConfigService borrowConfigService;

	private Map<String, Object> data = new HashMap<String, Object>();

	/**
	 * 逾期信息展示
	 * 
	 * @return
	 */
	@Action("/modules/loan/borrowLate/borrowLateManager")
	public String borrowLateManager() throws Exception {
		List<BorrowConfig> borrowConfigList = borrowConfigService
				.findAllNotFlowAndSecond();
		request.setAttribute("borrowConfigList", borrowConfigList);
		return "borrowLateManager";
	}

	/**
	 * 逾期信息列表
	 * 
	 * @return
	 */
	@Action("/modules/loan/borrowLate/borrowLateList")
	public void borrowLateList() throws Exception {
		model.setSize(paramInt("rows"));
		if (StringUtil.isBlank(paramString("status"))) {
			model.setStatus(BorrowRepaymentStatus.UN_KNOWN);
		}
		// 是否逾期
		model.setLate(true);
		PageDataList<BorrowRepaymentModel> list = borrowRepaymentService.list(model, searchName);
		data.put("total", list.getPage().getTotal());
		data.put("rows", list.getList());
		printJson(getStringOfJpaObj(data));
	}

	/**
	 * 逾期信息展示
	 * 
	 * @return
	 */
	@Action("/modules/loan/borrowLate/borrowLateEditPage")
	public String borrowLateEditPage() throws Exception {
		BorrowRepayment borrowRepayment = borrowRepaymentService.findById(model.getId());
		request.setAttribute("borrowRepayment", borrowRepayment);
		return "borrowLateEditPage";
	}

	/**
	 * 逾期垫付
	 * 
	 * @throws Exception
	 */
	@Action("/modules/loan/borrowLate/overdue")
	public void overdue() throws Exception {
		BorrowRepayment borrowRepayment = borrowRepaymentService.findById(model.getId());
		borrowRepaymentService.overdue(borrowRepayment, getOperator());
		data.put("result", true);
		data.put("msg", "操作成功！");
		printJson(getStringOfJpaObj(data));
	}

	public BorrowRepaymentModel getModel() {
		return model;
	}

}
